home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 208_01 / e0.c < prev    next >
Text File  |  1987-10-11  |  8KB  |  425 lines

  1. /*
  2. HEADER:        CUG208;
  3. TITLE:        'e' for CP/M68K
  4. VERSION:    1.48b
  5.  
  6. DESCRIPTION:    "a screen editor";
  7.  
  8. KEYWORDS:    editor;
  9. SYSTEM:        CP/M68K, V1.2;
  10. FILENAME:    e/e0.c
  11. WARNINGS:    "the default value is for systems with 128K bytes
  12.          of memory or more";
  13. SEE-ALSO:    cpm68k.c, e68k.doc, CUG VOL 133;
  14. AUTHORS:    G.N.Gilbert('e'), J.W.Haefner(for DeSmet C on MSDOS and UNIX)
  15. CODER:        Yoshimasa Tsuji
  16. COMPILERS:    DRI C(Alcyon C) for CP/M68K;
  17. */
  18.  
  19. /*
  20.     FUNCTIONS: main, initialise, edit, finish, askforfile,
  21.            xit
  22.     PURPOSE: initialise; process commands, finish
  23. */
  24.  
  25. #define DEF    1
  26. #include "e.h"
  27.  
  28. #include <setjmp.h>
  29. jmp_buf mainmenu;
  30.  
  31. main(argc,argv)
  32. char **argv;
  33. {
  34.     int startline = 1;
  35.     char *dig;
  36.  
  37.  
  38.     while (--argc)
  39.         if ( **++argv == '-') {
  40.             dig= *argv+1;
  41.             switch((int)*dig) {
  42.             case 'a' :
  43.                 autoin= !autoin;
  44.                 break;
  45.             case 'b' :
  46.                 backup=!backup;
  47.                 break;
  48.             case 'l' :    /* ell */
  49.                 displaypos=!displaypos;
  50.                 break;
  51.             case 'h' :
  52.                 blockscroll=!blockscroll;
  53.                 break;
  54.             case 'i' :
  55.                 hilight=!hilight;
  56.                 break;
  57.             case 't' :
  58.                 tabwidth=atoi(++dig);
  59.                 break;
  60.             case 's' :
  61.                 trail=!trail;
  62.                 break;
  63.             case 'v' :
  64.                 helpon=!helpon;
  65.                 break;
  66.             case 'd' :
  67.                 strcpy(pagingdir, dig+1);
  68.                 break;
  69.             default  :
  70.                 if (isdigit(*dig)) {
  71.                     startline = atoi(dig);
  72.                     break;
  73.                 }
  74.                 putstr("Illegal option: ");
  75.                 putstr(*argv);
  76.                 exit(1);
  77.             }
  78.         }
  79.         else strcpy(filename[0] ? name : filename, *argv);
  80.  
  81.     terminit();
  82.     keytranslate();
  83.  
  84.     for(;;) {
  85.         initialise(startline);
  86.         edit();
  87.         startline=1;
  88.     }
  89. }
  90.  
  91. initialise(startline)
  92. {
  93.     /*initialisations that are done before editing each new file: */
  94.     extern char *getwd();
  95.  
  96.     cursorx=charn=offset=lastoff=from=to=histptr=histcnt=ncommand=0;
  97.     cursory=topline=lastread=lastl=findir=1;
  98.     isdim=replace=repeat=blocking=blankedmess=storehist=NO;
  99.     pfirst= -100;
  100.     goteof=YES;
  101.     errmess=0;
  102.  
  103.     getwd(curdir);
  104.     initvm();
  105.  
  106.     text[0]='\0';
  107.     cline=1;
  108.     altered=YES;
  109.     puttext();
  110.  
  111.     if (filename[0]) {
  112.         clear();
  113.         gotoxy(8,11);
  114.         putstr("e  screen editor  version ");
  115.         putstr(VERSION);
  116.         putstr("  MICROLOGY 1983");
  117.         gotoxy(20,12);
  118.         terminal();
  119.  
  120.         while ((textfp= fopen(filename,"r")) == 0) {
  121.             error("Can't open file");
  122.             lastl=1;
  123.             askforfile();
  124.             if(!filename[0])goto newfile;
  125.         }
  126.         lastl=UNKNOWN;
  127.         lastread=0;
  128.         goteof=NO;
  129.         if (name[0]) {
  130.             strcpy(filename,name);
  131.             name[0]='\0';
  132.         }
  133.         format(filename);
  134.     }
  135. newfile:
  136.     errmess=0;
  137.     strcpy(text,getline(startline));
  138.     cline=loc(startline,0);
  139.     clear();
  140.     if (helpon) dohelp();
  141.     putpage();
  142. }
  143.  
  144. edit()        /*command processor*/
  145. {
  146.     int c;
  147.  
  148.     setjmp(mainmenu);
  149.  
  150.     for(;;) {
  151.         goodline=cline;
  152.         unmess();
  153.         ptlineno(cline);
  154.         resetcursor();
  155.         c=getkey();
  156.         if (errmess != 0)
  157.             errmess=0,
  158.             putstatusline(cline);
  159.         ncommand++;
  160.         storehist=YES;
  161.         if(goabout(c) == 0)     /* processed c */
  162.             continue;
  163.         switch(c) {
  164.         case DELLEFT    :
  165.             deletechar(-1);
  166.             break;
  167.         case DELRIGHT    :
  168.             deletechar(0);
  169.             break;
  170.         case DELLNKEY    :
  171.             text[0]='\0';
  172.             crdelete(cline == lastl ? -1 : 0);
  173.             break;
  174.         case DELWDKEY    :
  175.             deleteword();
  176.             break;
  177.         case ALTERKEY    :
  178.             replace=YES;
  179.             findorrep();
  180.             break;
  181.         case BLOCKKEY    :
  182.             blockops();
  183.             break;
  184.         case RDFILEKEY    :
  185.             putmess("File to read? ");
  186.             scans(name,FILELEN);
  187.             if (strlen(name) > 0) {
  188.                 readfile(name);
  189.                 putpage();
  190.             }
  191.             break;
  192.         case HELPKEY    :
  193.             (helpon = !helpon)? dohelp():unmess();
  194.             break;
  195.         case CR        :
  196.             crinsert(0);
  197.             break;
  198.         case CRSTILL    :
  199.             crinsert(-1);
  200.             break;
  201.         case ENVIRKEY   :
  202.             envir();
  203.             break;
  204.         case QUITKEY    :
  205.             if (finish()) return;
  206.             break;
  207.         case UNDOKEY    :
  208.             undo();
  209.             break;
  210.         case TAB:
  211.             insertchar('\t');
  212.             break;
  213.         case ESCKEY    :
  214.             resetcursor();
  215.             c=inchar();
  216.         default        :
  217.             insertchar(c);
  218.             break;
  219.         }
  220.     }
  221. }
  222. /* shared code between edit() and block operation */
  223. goabout(c)
  224. {
  225.     switch (c) {
  226.     case DOWNKEY    :
  227.         moveline(1);
  228.         break;
  229.     case UPKEY    :
  230.         moveline(-1);
  231.         break;
  232.     case LEFTKEY    :
  233.         movechar(-1);
  234.         break;
  235.     case RIGHTKEY    :
  236.         movechar(1);
  237.         break;
  238.     case LEFTWKEY    :
  239.         moveword(-1);
  240.         break;
  241.     case RIGHTWKEY    :
  242.         moveword(1);
  243.         break;
  244.     case BOLKEY    :
  245.         sync(0);
  246.         break;
  247.     case EOLKEY    :
  248.         sync(strlen(text));
  249.         break;
  250.     case UPPAGE    :
  251.         movepage(-1);
  252.         break;
  253.     case DOWNPAGE    :
  254.         movepage(0);
  255.         break;
  256.     case HOMEKEY    :
  257.         if (jumpline(lastl-cline)) sync(strlen(text));
  258.         break;
  259.     case BOFKEY    :
  260.         if (jumpline(1-cline)) sync(0);
  261.         break;
  262.     case JUMPKEY    :
  263.         { register char *aptr; register int sign;
  264.             putmess("Jump to? ");
  265.             scans(ans,6); aptr= ans;
  266.             if( *aptr == '+')sign= '+';
  267.             else if(*aptr == '-')sign = '-';
  268.             else sign = 0;
  269.             if(sign)aptr++;
  270.             to= atoi(aptr);
  271.             if (to > 0) {
  272.                 if(sign== '+')to += cline;
  273.                 else if(sign == '-') to -= cline;
  274.                 jumpline(to-cline);
  275.             }
  276.             break;
  277.         }
  278.     case FINDKEY    :
  279.         replace=NO;
  280.         findorrep();
  281.         break;
  282.     case REPKEY    :
  283.         repeat=YES;
  284.         dofindrep(1);
  285.         repeat=NO;
  286.         break;
  287.     default        :
  288.         return(-1);
  289.     }
  290.     return(0);
  291. }
  292.  
  293.  
  294. finish()    /*return YES to edit another file; NO to return to current file
  295.           or don't return, but exit if finished altogther */
  296. {
  297.     bool abandon;
  298.     int c;
  299.     char tempname[FILELEN], namebak[FILELEN];
  300.  
  301.     putmess("W|rite edited text to file, |A|bandon all edits, or |R|eturn? ");
  302.     while ( (c=getlow()) != 'w' && c != 'a' && c != 'r');
  303.     putch(c);
  304.     if (c == 'r') return(NO);
  305.  
  306.     abandon= c == 'a';
  307.     if (c == 'w') {
  308.         if (!filename[0]) {
  309.             putmess("File to write to? ");
  310.             scans(filename,FILELEN);
  311.             format(filename);
  312.             if (filename[0] <= ' ' || (!backup && !exists(filename)))
  313.             {
  314.                 filename[0]='\0';
  315.                 return(NO);
  316.             }
  317.         }
  318.         if (backup) {    /*delete old bak file*/
  319.             retag(strcpy(namebak,filename),"BAK");
  320.             if (checkexists(namebak) && unlink(namebak) == FAIL) {
  321.                 error("Can't delete backup file");
  322.                 return(NO);
  323.             }
  324.         }
  325.         strcpy(tempname,filename); /*keep old name in 'filename'*/
  326.         retag(tempname,"$$$"); /*new file called'.$$$'*/
  327.         if (writefile(1,lastl,tempname,filename,YES) == FAIL) {
  328.             unlink(tempname); return(NO);}
  329.         /*check original file still exists - may have been deleted or
  330.                    renamed by user */
  331.         if (checkexists(filename)) {
  332.             if (backup) {
  333.                 /*orig. file becomes '.bak' */
  334.                 if (rename(filename,namebak) == FAIL) {
  335.                     error("Can't rename old file to .BAK");
  336.                     goto failed;
  337.                 }
  338.             }
  339.             else {
  340.                 /*delete orig file*/
  341.                 if (unlink(filename) == FAIL) {
  342.                     error("Can't delete old file");
  343. failed:             /*if can't delete/rename old file,
  344.                     change new name to '.$$$'*/
  345.                     strcpy(filename,tempname);
  346.                     goto nowrite;
  347.                 }
  348.             }
  349.         }
  350.         rename(tempname,filename); /*new file goes from '$$$' to orig name*/
  351.     }
  352. nowrite:
  353.     putmess("E|xit from editor, |R|eturn to this file, or edit |A|nother file? ");
  354.     while ( (c=getlow()) != 'e' && c!='a' && c!='r');
  355.     putch(c);
  356.     switch(c) {
  357.     case 'e' :
  358.         if (pagefd != NOFILE)
  359.             close(pagefd),
  360.             unlink(pagingfile);
  361.         xit();
  362.     case 'a' :
  363.         if(textfp)
  364.             fclose(textfp);
  365.         if (pagefd != NOFILE)
  366.             close(pagefd),
  367.             unlink(pagingfile);
  368.         askforfile();
  369.         return(YES);
  370.     case 'r' :
  371.         if (!abandon) {
  372.             gotoxy(WAITPOS,0);
  373.             textfp= fopen(filename,"r");
  374.             lastl=UNKNOWN;
  375.             lastread=0;
  376.             goteof=NO;
  377.             initvm();
  378.             strcpy(text,getline(cline));
  379.             errmess=0;
  380.             putstatusline(cline);
  381.         }
  382.         return(NO);
  383.     }
  384. }
  385.  
  386. xit()
  387. {
  388.     deleteline(0,23);
  389.     gotoxy(0,22);
  390.     termfini();    /*close down terminal*/
  391.     exit(0);
  392. }
  393.  
  394. askforfile()    /*get another file to edit into 'filename' */
  395. {
  396.     char *p, *index();
  397.  
  398.     for(;;) {
  399.         printdirectory(curdir);
  400.         putstr("Name of file to edit     |or [return] to create a new file,\n");
  401.         putstr(" |pathname/ to change directory,\n");
  402.         putstr(" |[escape] to exit)|          ? ");
  403.         if (scans(filename,FILELEN) == ESCKEY) xit();
  404.         p = filename;
  405.         while(*p)p++;
  406.         if(*--p == '/') {
  407.             *p = 0;
  408.             if(chdir(filename) != FAIL)
  409.                 strcpy(curdir,filename);
  410. #if CPM68K
  411.             __BDOS(13);    /*reset all drives*/
  412. #endif
  413.             continue;
  414.         }
  415.         name[0]='\0';
  416.         return;
  417.     }
  418. }
  419.  
  420.  
  421.